package com.g4.shoppingsingle.controller;

import com.g4.shoppingsingle.common.Result;
import com.g4.shoppingsingle.pojo.DdCommentInfo;
import com.g4.shoppingsingle.service.DdCommentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
//@RequestMapping()
@RequestMapping("/shoppingsingle")
public class DdCommentInfoController {
    /**
     * 返回用户评论页面视图
     * @param pdId
     * @return
     */
    @GetMapping("/commentInfo/{pdId}")
    public ModelAndView commentInfo(@PathVariable Integer pdId){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("comment");
        return mv;
    }

    @Autowired
    private DdCommentInfoService ddCommentInfoService;

    /**
     * 添加评论
     * @param ddCommentInfo
     * @return
     */
    @PostMapping("/addComment")
    public Result addComment(@RequestBody DdCommentInfo ddCommentInfo){
        log.info("添加评论"+ddCommentInfo);
        ddCommentInfoService.addComment(ddCommentInfo);
        log.info("添加评论成功");

        return new Result(true, "操作成功");
    }

    /**
     * 删除评论
     * @param ddCommentInfo
     * @return
     */
    @PostMapping("/deleComment")
    public Result deleComment(@RequestBody DdCommentInfo ddCommentInfo){
        log.info("删除评论,id="+ddCommentInfo.getId());
        ddCommentInfoService.deleComment(ddCommentInfo);
        return new Result(true, "操作成功");
    }

    /**
     * 查询评论
     * @param ddCommentInfo
     * @return
     */
    @PostMapping("/selectComment")
    public Result selectComment(@RequestBody DdCommentInfo ddCommentInfo){
        log.info("查询商品评论,id="+ddCommentInfo.getPdId());
        List<DdCommentInfo> ddCommentInfos = ddCommentInfoService.selectComment(ddCommentInfo);
        //创建集合
        ArrayList<Object> objects = new ArrayList<>();
        //添加查询结果的长度
        objects.add(ddCommentInfos.size());
        //添加查询结果
        objects.add(ddCommentInfos);

        return new Result(true,"操作成功",objects);
    }

}
